package com.db.compare.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.db.compare.exp.RbacOrgExp;
import com.db.compare.model.RbacOrg;
import com.db.compare.mapper.RbacOrgMapper;
import com.db.compare.service.RbacOrgService;
import com.db.compare.vo.RbacOrgVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class RbacOrgServiceImpl implements RbacOrgService {
    @Autowired
    private RbacOrgMapper rbacorgMapper;
    @Override
    public PageInfo<RbacOrg> pageInfo(RbacOrgVo rbacorgVo) {
        PageHelper.startPage(rbacorgVo.getPageNum(),rbacorgVo.getPageSize());
        List<RbacOrg> rbacorgList = rbacorgMapper.findList(rbacorgVo);
        PageInfo<RbacOrg> pageInfo = new PageInfo<>(rbacorgList);
        return pageInfo;
    }

    @Override
    public void download(String ids, HttpServletResponse response) throws Exception {
        String fileName = "组织管理";
        try {
            ServletOutputStream outputStream=Appcontext.downloadResponse(response,fileName);
            if(StringUtils.isBlank(ids)){
                EasyExcel.write(outputStream, RbacOrgExp.class).sheet(fileName).doWrite(null);
            }else{
                String[] arrays = ids.split(",");
                List<RbacOrg> rbacorgList = rbacorgMapper.findByIds(arrays);
                List<RbacOrgExp> rbacorgExpList = new ArrayList<>();
                for(RbacOrg rbacorg:rbacorgList){
                    RbacOrgExp rbacorgExp = new RbacOrgExp();
                    BeanUtils.copyProperties(rbacorg,rbacorgExp);
                    rbacorgExpList.add(rbacorgExp);
                }
                EasyExcel.write(outputStream, RbacOrgExp.class).sheet(fileName).doWrite(rbacorgExpList);
            }
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean upload(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        try {
            ExcelImportResult<RbacOrgExp> result = ExcelImportUtil.importExcelMore(file.getInputStream(), RbacOrgExp.class,
                    importParams);
            List<RbacOrgExp> rbacorgExps = result.getList();
            for (RbacOrgExp rbacorgExp : rbacorgExps) {
                RbacOrg rbacorg = new RbacOrg();
                BeanUtils.copyProperties(rbacorg,rbacorgExp);
                int i = rbacorgMapper.insert(rbacorg);
                if(i < 1){
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
